
package com.dtguai.app.config.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

/**
  * @author: guoLiang
  * @date：2018-09-29 03:46:03
  */
@Configuration
public class RedisConfiguration {

   @Autowired
   private RedisConnectionFactory factory;

   @Bean
   public RedisTemplate<String, Object> redisTemplate() {
       RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
       redisTemplate.setKeySerializer(new StringRedisSerializer());
       redisTemplate.setHashKeySerializer(new StringRedisSerializer());
       redisTemplate.setHashValueSerializer(new StringRedisSerializer());
       redisTemplate.setValueSerializer(new StringRedisSerializer());
       redisTemplate.setConnectionFactory(factory);
       return redisTemplate;
   }

   @Bean
   @ConditionalOnMissingBean(StringRedisTemplate.class)
   public StringRedisTemplate stringRedisTemplate() throws UnknownHostException {
       StringRedisTemplate template = new StringRedisTemplate();
       template.setConnectionFactory(factory);
       return template;
   }

   @Bean
   public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
       return redisTemplate.opsForHash();
   }

   @Bean
   public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
       return redisTemplate.opsForValue();
   }

   @Bean
   public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
       return redisTemplate.opsForList();
   }

   @Bean
   public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
       return redisTemplate.opsForSet();
   }

   @Bean
   public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
       return redisTemplate.opsForZSet();
   }
}
